<template>
    <el-dialog :title="isEdit?'编辑船舶资料':'新增船舶资料'" :visible.sync="dialogForm" width="680px">
        <el-form :inline="true" ref="form" :model="form" :rules="rules" class="add-form" label-width="90px">
            <el-row>
                <el-col :span="12">
                    <el-form-item label="船舶代码" prop="shipCode">
                        <el-input :disabled="isEdit" v-model.trim="form.shipCode" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="船舶名称" prop="shipCName">
                        <el-input v-model.trim="form.shipCName" clearable></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="船舶类型" prop="shipType">
                        <!--<auto-search-dic ref="shipType" @select="v=>form.shipType=v" :name="form.shipType"-->
                        <!--code="SHIP_TYPE"></auto-search-dic>-->
                        <el-select v-model="form.shipType" placeholder="请选择" clearable>
                            <el-option v-for="[key, val] in _dictShipType" :key="key" :label="val" :value="key">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="英文名称">
                        <el-input v-model.trim="form.shipEName" clearable></el-input>
                    </el-form-item>
                    <!--<p v-if="letter" style="color: #ff0000;font-size: 12px;margin: -24px 0 0 90px;">只能输入字母</p>-->
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="船公司" prop="shipOwnerName">
                        <auto-search-custom ref="shipOwnerName" :name="form.shipOwnerName" :isShipCompany="true"
                                            @matchRecord="handleSelectShipOwner"></auto-search-custom>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="船代" prop="shipAgentName">
                        <auto-search-custom ref="shipAgentName" :name="form.shipAgentName" :isShipAgent="true"
                                            @matchRecord="handleSelectShipAgent"></auto-search-custom>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="关薄号" prop="closetNumber">
                        <el-input v-model.trim="form.closetNumber" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="海关EDI代码" prop="ediCode">
                        <el-input v-model.trim="form.ediCode" clearable></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="最大贝数" prop="maxBay">
                        <el-input-number style="width: 188px" :controls="false" v-model="form.maxBay"
                                         clearable></el-input-number>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="最大排数" prop="maxRow">
                        <el-input-number style="width: 188px" :controls="false" v-model="form.maxRow"
                                         clearable></el-input-number>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="最大层高" prop="maxTier">
                        <el-input-number style="width: 188px" :controls="false" v-model="form.maxTier"
                                         clearable></el-input-number>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row v-show="isEdit">
                <el-col :span="12">
                    <el-form-item label="创建人" prop="createBy">
                        <el-input v-model.trim="form.createBy" disabled></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="创建时间" prop="createDateTime">
                        <el-input v-model.trim="form.createDateTime" disabled></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogForm = false">取 消</el-button>
            <el-button type="primary" @click="handleSubmit" :loading="$store.getters.loadingBtn">保 存</el-button>
        </div>
    </el-dialog>
</template>

<script>
    import { addShip, updateShip } from "@/api/port/port";
    import autoSearchDic from '@/components/autocomplete/autoSearchDic'
    import autoSearchCustom from '@/components/autocomplete/autoSearchCustom'
    import { REG_CHINESE, REG_UPPER_WORD, PRIVATE_LETTER, REG_NUM_CODE } from "@/utils/Reg";
    import { fiveLength, sortLength } from "@/utils/rules";

    export default {
        name: "shipForm",
        components: {
            autoSearchDic, autoSearchCustom
        },
        data() {
            return {
                form: this.clearForm(),
                rules: {
                    shipCode: [{ required: true, message: '请输入船舶代码', trigger: 'change' }],
                    shipCName: [{ required: true, message: '请输入船舶名称', trigger: 'change' }, fiveLength],
                    shipType: [{ required: true, message: '请选择船舶类型', trigger: 'change' }],
                    // shipEName: [
                    //     { validator: PRIVATE_LETTER, trigger: 'change' }, sortLength],
                    closetNumber: [sortLength],
                    ediCode: [sortLength],
                },
                dialogForm: false,
                isEdit: false,
                id: '',
                letter: true
            }
        },
        methods: {
            clearForm() {
                return {
                    shipCode: '',
                    shipCName: '',
                    shipType: '',
                    shipEName: '',
                    shipOwnerName: '',
                    shipAgentName: '',
                    closetNumber: '',
                    ediCode: '',
                    maxBay: '',
                    maxRow: '',
                    maxTier: '',
                    createBy: '',
                    createDateTime: '',
                }
            },
            // 保存
            handleSubmit() {
                if (Number(this.form.maxBay) % 2 === 0) {
                    this.$message({ message: '贝数应该为奇数', type: 'error' })
                    return
                }
                this.$refs.form.validate((valid) => {
                    if (valid) {
                        this.isEdit ? this.handleToEdit() : this.handleToAdd()
                    } else {
                        this.$message({
                            message: '请检查表单',
                            type: 'error'
                        })
                    }
                })
            },
            // 新增
            handleToAdd() {
                addShip(this.form).then(res => {
                    this.$message({ message: '新增成功!', type: 'success' })
                    this.dialogForm = false
                    this.$emit('load')
                })
            },
            // 编辑
            handleToEdit() {
                updateShip(this.id, this.form).then(res => {
                    this.$message({ message: '编辑成功!', type: 'success' })
                    this.dialogForm = false
                    this.$emit('load')
                })
            },
            handleSelectShipOwner(item) {
                this.form.shipOwnerName = item.name
                this.form.shipOwnerCode = item.customerCode
                this.form.shipOwnerId = item.id
            },
            handleSelectShipAgent(item) {
                this.form.shipAgentName = item.name
                this.form.shipAgentCode = item.customerCode
                this.form.shipAgentId = item.id
            }
        },
        watch: {
            dialogForm(bool) {
                if (!bool) {
                    this.$refs.shipOwnerName.handleClear()
                    this.$refs.shipAgentName.handleClear()
                    // this.$refs.shipType.handleClear()
                    this.$refs.form.resetFields()
                    this.isEdit = false
                    this.form = this.clearForm()
                    this.$emit('clear')
                }
            },
        },
    }
</script>

<style scoped>

</style>
